package com.zxgllhh.db;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

import com.zxgllhh.utils.PropertyUtils;

public class DataSourceFactory {

	private static BasicDataSource ds = null;

	static {
		ds = new BasicDataSource();
		ds.setDriverClassName(PropertyUtils.getProperty("jdbc.driverClassName"));
		ds.setUrl(PropertyUtils.getProperty("jdbc.url"));
		ds.setUsername(PropertyUtils.getProperty("jdbc.username"));
		ds.setPassword(PropertyUtils.getProperty("jdbc.password"));

		//ds.setDefaultAutoCommit(true);
		//ds.setDefaultReadOnly(true);
		//初始化连接
		ds.setInitialSize(20);
		//最大空闲连接
		ds.setMaxIdle(20);
		//最小空闲连接
		ds.setMinIdle(10);
		//最大连接数
		ds.setMaxActive(50);
	}

	private DataSourceFactory() {

	}

	public static synchronized Connection getConnectionByDataSource() {
		Connection connection = null;
		try {
			connection = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
}
